package com.librarySystem.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.librarySystem.dao.UserDaoInterface;
import com.librarySystem.dto.UserDTO;
import com.librarySystem.entity.User;

@Service
public class UserService implements UserServiceInterface{
	
	@Resource
	private UserDaoInterface userDao;
	
	@Transactional
	@Override
	public User login(User user){
			User result = userDao.login(user);
			if(result != null){
				if(result.getPassWord().equals(user.getPassWord())){
					return result;
				}
			}
		
		return null;
	}

	@Override
	@Transactional
	public User register(User user) {
		int result = userDao.register(user);
		if(result == 0){
			return null;
		}else{
			user.setId(result);
		}
		return user;
	}

	@Override
	@Transactional
	public boolean updataPassWord(User user, UserDTO userDTO) {
		User result = userDao.login(user);
		if(result != null){
			if(result.getPassWord().equals(user.getPassWord())){
				user.setId(result.getId());
				user.setPassWord(userDTO.getNewPassWord());
				return userDao.updataPassWord(user);
			}
		}
		return false;
	}

}
